<?php

namespace Biz\service\BrandRights;

use Biz\model\brandRights\BrandRightsRecored;
use Doraemon\tools\Log;

class BrandRightsRecoredService
{

    public const SUB = 1;
    public const ADD = 2;

    /**
     * 添加记录
     * @param $brandId
     * @param $rightsName
     * @param $rightsId
     * @param $recordType
     * @param $sourceId
     * @param $sourceInfo
     * @param $operateData
     * @param $operateUserType
     * @param int $adminId
     * @return bool
     */
    public static function insertRecord(
        $brandId,
        $rightsName,
        $rightsId,
        $recordType,
        $sourceId,
        $sourceInfo,
        $operateData,
        $operateUserType,
        int $adminId = 0
    ): bool {
        $inData['operate_uid'] = $adminId;
        $inData['brand_id'] = $brandId;
        $inData['rights_name'] = $rightsName;
        $inData['rights_source_id'] = $rightsId;
        $inData['recored_type'] = $recordType;
        $inData['source_id'] = $sourceId;
        $inData['source_data'] = json_encode($sourceInfo);
        $inData['operate_data'] = $operateData;
        $inData['operate_user_type'] = $operateUserType;
        $res = BrandRightsRecored::insertData($inData);
        if (!$res) {
            Log::debug('消费权益-写入记录异常:', $inData);
            return false;
        }
        return true;
    }

    /**
     * 根据订单编号获取权益记录
     * @param $dsn
     * @return array
     */
    public static function getRecordByDsn($brandId,$dsn){
        $where['brand_id'] = $brandId;
        $where['source_id'] = $dsn;
        $record = [];
        $result = BrandRightsRecored::getInfo($where,'*');
        if($result && is_array($result)){
            $record = $result;
        }
        return $record;
    }
}